{{--注册 模板--}}
@extends('web.main')
@section('content')
<div class="dbody">
    <div class="center plate">
        <div class="pTitle">
            <a href="/web/login">登陆</a>
            <a href="javascript:;" class="click">注册</a>
        </div>
        <div style="height:10px;"></div>
        <div class="pInput">
            <input onkeyup ="clearError();"  type="text" id="mobile"  placeholder="请输入您的手机号">
            <span id="error"></span>
        </div>
        
        <div class="pInput">
            <input type="text" onkeyup="clearErrorCode();" id="yzm" placeholder="请输入您的验证码">
            <input type="button" id="getYzm" onclick="sendYzm()" value="获取验证码">
            <span id="errorCode"></span>
        </div>
        <div class="pInput">
            <input type="text" onkeyup="numberSum();" id="password" placeholder="请输入您的密码">
            <span id="errorpwd"></span>
        </div>
        <div class="pInput">
            <input type="button" id="submit" onclick="setSubmit()" value="注册">
        </div>
        <div class="pInput">
            <p class="agree">
                注册即表示同意
                <a href="javascript:;"
                   onclick="getAgree()"
                          style="padding:0">
                    用户协议和隐私条款
            </a>
            </p>
        </div>
        <div style="height:20px;"></div>
    </div>
</div>

<div id="webPop"></div>


<script type="application/javascript">
    $( function(){
        var sessionval = "{{ $uid }}";
        console.log(sessionval);
        if(sessionval!="noval"){
            getWebPop("即将跳转到下一页",'',0);
                            setTimeout(function(){
                            window.location.href = '/web/maininfo/{{$token}}';
                        }, 1000 * 2);
        }
    } );
    
    //发送验证码
    var phoneFlag = false;
    var passwordFlag = false;
    var codeFlag  = false;
    var sendFlag = false;
    function sendYzm(){
        var phone = $("#mobile").val();
        if(phone==""){
            //$("#error").text("请输入正确的手机号码");
            getWebPop('请输入正确的手机号码！','',0);
            return;
        }else{
            var validFlag = validPhone(phone);
            if(validFlag){
                sendPhone(phone);
            }
        }
        //getWebPop('功能待完善！','',0);
    }
    var showNumber = 0;
    function numberSum(){
        var sumValue = $("#password").val();
        if(sumValue.length<5){
            //$("#errorpwd").text("密码长度必须大于等于5");
            passwordFlag = false;
            if(showNumber==0){
                getWebPop('密码长度必须大于等于5！','',0);
            }
            showNumber++;
        }else{
            //$("#errorpwd").text("");
            passwordFlag = true;
        }
    }
    function clearErrorCode(){
        var yzm = $("#yzm").val();
        if(yzm.length==4){
            codeFlag = true;
        }else{
            codeFlag = false;
        }
    }
    function getDomain(){
        return '/api/member/';
    }
    //发送手机号码到后台获取验证码
    function sendPhone(phone){
        var data = {
            app_access:web_access,
            phone:phone
        };
        $.ajax({
            type:"post",
            data:data,
            url:getDomain()+'yzm',
            success:function(res){
                if(res.code==0){
                    //$("#errorCode").text(res.msg);
                    getWebPop(res.msg,'',0);
                    sendFlag = true;
                    phoneFlag = true;
                    //可以做成功后的事，zwp
                }else{
                    //$("#errorCode").text(res.msg);
                    getWebPop(res.msg,'',0);
                }
            },
            error:function(){
                //$("#errorCode").text("发送出错!");
                getWebPop("发送出错!",'',0);
            }
        });
    }
    
    //验证手机号码符合格式
    function validPhone(phone){
        var regx = new RegExp("^1[0-9]{10}$");
        if(!regx.test(phone)){
            //$("#error").text("请输入正确的手机号码");
            getWebPop("请输入正确的手机号码!",'',0);
            return false;
        }
        return true;
    }
    
    //清空手机号码为空信息
    function clearError(){
        //var errorText = $("#error").text();
        codeFlag = false;
        sendFlag = false;
        /*
        if(errorText=="请输入正确的手机号码"){
            $("#error").text("");
            return;
        }else{
            return;
        }
        */
    }
    //获取用户协议
    function getAgree(){
        getWebPop('功能待完善！','',0);
    }
    //页面跳转
    function setSubmit(){
        console.log(phoneFlag,passwordFlag,codeFlag,sendFlag);
        var phone = $("#mobile").val();
        var code = $("#yzm").val();
        var pwd = $("#password").val();
        
        if(phone.length>0){
            var validFlag = validPhone(phone);
            if(!validFlag){
                return;
            }
            if(!sendFlag){
                getWebPop("请先获取验证码",'',0);
                return;
            }
        }else{
            phoneFlag = false;
        }
        
        if(phoneFlag==false){
            getWebPop("手机号码不符合格式",'',0);
            return;
        }
        
        if(code.length!=4){
            getWebPop("验证码不符合格式",'',0);
            codeFlag = false;
            return;
        }else{
            codeFlag = true;
        }
        if(codeFlag==false){
            getWebPop("验证码不符合格式",'',0);
            return;
        }
        if(pwd.length<5){
            getWebPop("密码不符合格式",'',0);
            passwordFlag = false;
        }else{
            var regx = new RegExp("^[0-9a-zA-Z_]{5,20}$");//密码验证规则
            if(!regx.test(pwd)){
                getWebPop('密码格式不对，请输入5-20位数字/字母/符号','',0);
                passwordFlag = false;
            }else{
                passwordFlag = true;
            }
            
        }
        
        if(passwordFlag==false){
            getWebPop("密码不符合格式",'',0);
            return;
        }
        if(phoneFlag&&passwordFlag&&codeFlag){
            
            var data = {
                phone:phone,
                pwd:pwd,
                code:code,
                app_access:web_access
            };
            $.ajax({
                type:"post",
                url:"/api/web/user/add",
                data:data,
                success:function(res){
                    
                    if(res.code==200){
                        var token = res.data.token;
                        getWebPop(res.message+"即将跳转到下一页",'',0);
                        setTimeout(function(){
                        window.location.href = '/web/maininfo/'+token;
                    }, 1000 * 2);
                    }else{
                        getWebPop(res.message,'',0);
                    }
                },
                error:function(){
                    getWebPop("操作失败",'',0);
                }
            });
        }
    }
    
</script>
@stop